package yl.hs.bmipfull.daos.material;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import yl.hs.bmipfull.daos.BaseDao;
import yl.hs.bmipfull.pojo.material.PurchaseReceiptEntity;
import yl.hs.bmipfull.pojo.material.PurchaseReceiptIEntity;
import yl.hs.bmipfull.viewmodels.dataCockpit.RawMaterialProcurement;
import yl.hs.bmipfull.viewmodels.report.MonthlyRawReportVO;
import yl.hs.bmipfull.viewmodels.report.RawCostAnalysisVO;
import yl.hs.bmipfull.viewmodels.report.RawMaterialStatisticModel;
import yl.hs.bmipfull.viewmodels.report.RawPurchaseStatisticVO;
import yl.hs.bmipfull.viewmodels.salemanmage.SetTimeVO;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作用:  采购产品单据信息持久层接口<br/>
 * 创建时间: 2021年05月19日 17时26分05秒 <br/>
 *
 * @author 吴高丰
 */
@Repository
public interface IPurchaseReceiptDao extends BaseDao<PurchaseReceiptEntity> {

    /**
     * 查询采购产品单据信息信息
     *
     * @param map 查询条件 {@link PurchaseReceiptEntity}
     * @return 采购产品单据信息-List<{@link PurchaseReceiptEntity}>
     */
    List<PurchaseReceiptEntity> queryList(@Param("map") Map<String, Object> map,List<Integer> states,@Param("filters") List<Map<String,Object>> filters);

    /**
     * 分页查询采购产品单据信息信息
     *
     * @param map 查询条件 {@link PurchaseReceiptEntity}
     * @return 站内信息-List<{@link PurchaseReceiptEntity}>
     */
    List<PurchaseReceiptEntity> queryPage(@Param("map") Map<String, Object> map,@Param("filters") List<Map<String,Object>> filters,List<Integer> states);

    /**
     * 根据查询条件统计数据数量
     *
     * @param query 查询条件
     * @return 数据数量
     */
    Integer queryCountByCondition(@Param("map") Map<String, Object> query,@Param("filters") List<Map<String,Object>> filters,List<Integer> states);

    /**
     * 根据采购产品单据信息编号修改状态
     *
     * @param snArray 采购产品单据信息编号数组
     * @param state
     * @return
     */
    Integer changeStateBySn(String[] snArray, short state);

    /**
     * 查询已完成入库的车牌信息
     */
    List<String> queryAllTruckPlate();

    /**
     * 根据单据状态，条数，排序字段查询单据信息
     */
    List<PurchaseReceiptEntity> queryByState(short state, int top, String sort, String key);
    Integer updateOrderPrice(BigDecimal price, List<String> snList);
    /**
     * 根据车牌号查询最新一车信息
     *
     * @param truckPlate
     * @return
     */
    PurchaseReceiptEntity queryByTruckPlate(String truckPlate);
    PurchaseReceiptEntity queryByTruckPlateByState(String truckPlate);

    Integer updateOrderToReviewed(@Param("orderSn")String orderSn, String userSN, String userName, boolean review, short code,BigDecimal reviewWeight,BigDecimal reviewQuantity,BigDecimal reviewPrice);

    List<PurchaseReceiptEntity> queryGroupBySupplierSN(@Param("map") Map<String, Object> map,@Param("filters") List<Map<String,Object>> filters,List<Integer> states);

    /**
     * deleteByOpenId
     * @return
     */
    int deleteByOpenId(String openId);

    List<RawMaterialStatisticModel> queryGroupByRaw(@Param("map") Map<String, Object> map, @Param("filters") List<Map<String,Object>> filters, List<Integer> states);
    /**
     * 原材购进汇总
     * @param map 查询信息
     * @param productCateSn 产品类型sn
     * @return 原材购进汇总信息
     */
    List<RawPurchaseStatisticVO> queryRawPurchaseStatistic(@Param("map") Map<String, Object> map, String productCateSn);

    /**
     * 获取原材价格
     * @param contractSn 合同sn
     * @param rmSn 原材sn
     * @param inTime 入库时间
     * @return 价格
     */
    BigDecimal getRmPrice(String contractSn, String rmSn, Date inTime);

    /**
     * 获取发货单的发货年份和月份
     * @return 时间
     */
    List<SetTimeVO> getSetTime();

    /**
     * 采购月报  行数
     * @param map
     * @param productSn
     * @param filters
     * @param states
     * @return
     */
    Integer getMonthlyRawReportCount(@Param("map") Map<String, Object> map,String productSn,@Param("filters") List<Map<String,Object>> filters, List<Integer> states);

    /**
     * 采购月报数据
     * @param map
     * @param productSn
     * @param filters
     * @param states
     * @return
     */
    List<MonthlyRawReportVO> getMonthlyRawReport(@Param("map") Map<String, Object> map,String productSn,@Param("filters") List<Map<String,Object>> filters, List<Integer> states);

    List<PurchaseReceiptEntity> querySupplierGroupByRmCateSN(@Param("map") Map<String, Object> map,String productSn,@Param("filters") List<Map<String,Object>> filters, List<Integer> states) ;

    List<RawCostAnalysisVO> getRawCostAnalysis(@Param("map") Map<String, Object> map,String productSn,@Param("filters") List<Map<String,Object>> filters, List<Integer> states);

    /**
     * 获取未审核
     * @return 行数
     */
    @Select("SELECT COALESCE(COUNT(0),0) from purchase_receipt WHERE state=1")
    Integer getNotApproved();

    /**
     * 获取已审核的单据金额
     * @return 汇总
     */
    @Select("SELECT COALESCE(SUM(getRMPrice ( contractSN, rmSN, outTime )*netWeight),0) from purchase_receipt WHERE state=10")
    BigDecimal getExamineNetWeight();
    @Select("select IFNULL(sum(amount),0)  from purchase_receipt where state>0 and contractSN=#{contractSN} and QUARTER(outTime)=QUARTER(now()) and year(outTime)=year(now())")
    BigDecimal gettatileByContractSn(String contractSN);
    List<PurchaseReceiptIEntity> getPurchaseByMonth();

    @Select("select contractSN FROM purchase_receipt WHERE  !ISNULL(contractSN) and state>=1 and rmCateSN=#{cateSN}  GROUP BY contractSN")
    List<String> getContractSNByCateSN(String cateSN);

    PurchaseReceiptEntity findTruckTempOrder(@Param("truck") String truck);
    //原材采购
    List<RawMaterialProcurement> getProcurementSituation(String inTime);
    //原材耗材
    List<RawMaterialProcurement> getConsumables( String outTime);

    BigDecimal getPriceTrend(Date endTime, String s);

    List<RawMaterialProcurement> getPurchaseVolume(String timeType);

    List<RawMaterialProcurement> getAnnularPurchaseVolume(String timeType);

    List<RawMaterialProcurement> getPurchaseCost(String timeType);

    List<RawMaterialProcurement> getPurchaseAnnular(String timeType);

    Integer updateInHost(List<String> snList,int state);

    List<PurchaseReceiptEntity> queryListByWhSNAndTime(@Param("start") Date day,@Param("end") Date endTime,@Param("snList") List<String> whSNList);

    List<PurchaseReceiptEntity> queryUnitReceipts(@Param("unit") String unit,@Param("start") Date monthStartTime,@Param("end") Date monthEndTime);
    List<PurchaseReceiptEntity> averagePriceOfMaterials(@Param("map") Map<String, Object> map);

    List<PurchaseReceiptEntity> queryListByContractSn(String contractSn);
    @Update("update purchase_receipt set netWeight = #{netWeigt},blockWeight = #{blockWeight},orgWeight=#{orgWeight} where sn = #{sn}")
    Integer updateNetWeightBuSN(String sn,BigDecimal netWeigt,BigDecimal blockWeight,BigDecimal orgWeight);
    BigDecimal getTotalReviewWeightBySettleDetailSn(String settleDetailSn);
    Integer updateUnloading(String sn,BigDecimal unloadingPrice,BigDecimal unloadingMoney,int unloadingState);
    List<PurchaseReceiptEntity> quantityByRmSn(String rmSN);
}